<template>
  <div class="tab-tencent-main">
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" label-width="auto" size="default">

      <el-form-item label="EndPoint" prop="endpoint">
        <el-input type="text" v-model="ruleForm.endpoint" placeholder="EndPoint" />
      </el-form-item>

      <el-form-item label="KEY_ID" prop="secret_id">
        <el-input type="text" v-model="ruleForm.secret_id" placeholder="请输入OSS_ACCESS_KEY_ID" />
      </el-form-item>

      <el-form-item label="KEY_SECRET" prop="secret_key">
        <el-input type="text" v-model="ruleForm.secret_key" placeholder="请输入OSS_ACCESS_KEY_SECRET" />
      </el-form-item>

      <el-form-item label="区域" prop="region">
        <el-input type="text" v-model="ruleForm.region" placeholder="请输入应用所在区域" />
      </el-form-item>

      <el-form-item label="存储桶" prop="bucket_name">
        <el-input type="text" v-model="ruleForm.bucket_name" placeholder="请输入存储桶名称" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit" :loading="loading">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { fetchDictByType, updateCos } from '@/api'

let ruleFormRef = ref()
let ruleForm = reactive({})
let loading = ref(false)


//钩子
onMounted(() => {
  //查询数据
  requestIdleCallback(async () => {
    fetchDictByType('cosConfig/AL').then(data => {
      Object.assign(ruleForm, data)
    })
  })

})

const submit = async () => {
  loading.value = true
  try {

    //  if(ruleForm?.cos_server_type || ruleForm?.cos_server_type === ''){
    //     ruleForm.cos_server_type = 'Tencent'
    //  }

    ruleForm.cos_server_type = 'AL'

    updateCos(ruleForm).then(resp => {
      ElMessage.success('提交成功')
    })
  } catch (error) {
    console.log(error)
    ElMessage.error('提交失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

</script>

<style>
.tab-tencent-main {
  height: 100%;
}
</style>
